<?php

require_once 'pg_info.php';

class DatabaseUtils {
	private $conn;
	
	private static $instance;
	
	function __construct() {
		global $dbHost, $dbName, $dbUser, $dbPassword;
		try{
			$this->conn = new PDO("mysql:host=".$dbHost.";dbname=".$dbName, $dbUser, $dbPassword);
			$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		}catch(PDOException $e) {
		    log('ERROR: ' . $e->getMessage());
		}
	}
	
	function __destruct(){
		unset($this->conn);
	}
	
	private function runQuery($q, $params){
		try {
			$stmt = $this->conn->prepare($q);
			$stmt->execute($params);
			
			return($stmt);
		}catch(PDOException $e) {
		    log('ERROR: ' . $e->getMessage());
		    return(null);
		}
	}
	
	function runSelect($q,$params){
		$stmt = $this->runQuery($q,$params);
		if(!is_null($stmt))
			return($stmt->fetchAll());
		else
			return array();
	}
	
	function runInsert($q, $params){
		$stmt = $this->runQuery($q,$params);
		if(!is_null($stmt))
			return($stmt->rowCount());
		else
			return -1;
	}
	
	function runUpdate($q, $params){
		return($this->runInsert($q,$params));
	}
	
	function runDelete($q, $params){
		return($this->runInsert($q,$params));
	}
}

?>